package com.sixbro.shiro.common.config;

import com.sixbro.shiro.common.exception.BizException;
import com.sixbro.shiro.mapper.GeneratorMapper;
import com.sixbro.shiro.mapper.SysGeneratorMysqlMapper;
import com.sixbro.shiro.mapper.SysGeneratorOracleMapper;
import com.sixbro.shiro.mapper.SysGeneratorSqlServerMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

/**
 * <p>
 * 数据库配置
 * </p>
 *
 * @Author: Mr.Lu
 * @Since: 2020/6/19 13:31
 */
@Configuration
public class DbConfig {

    @Value("${app.database}")
    private String database;
    @Autowired
    private SysGeneratorMysqlMapper sysGeneratorMysqlMapper;
    @Autowired
    private SysGeneratorOracleMapper sysGeneratorOracleMapper;
    @Autowired
    private SysGeneratorSqlServerMapper sysGeneratorSqlServerMapper;

    @Bean
    @Primary
    public GeneratorMapper getGeneratorMapper(){
        if("mysql".equalsIgnoreCase(database)){
            return sysGeneratorMysqlMapper;
        }else if("oracle".equalsIgnoreCase(database)){
            return sysGeneratorOracleMapper;
        }else if("sqlServer".equalsIgnoreCase(database)){
            return sysGeneratorSqlServerMapper;
        }else {
            throw new BizException("不支持当前数据库：" + database);
        }
    }
}
